package com.ponhu.ea2.goodsCenter.provider.support.validation;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.Data;

import java.text.MessageFormat;
import java.util.List;

/**
 * 实体校验结果
 *
 * @author Wang
 * @date 2021/11/10 3:11 下午
 */
@Data
public class ValidationResult {
    /**
     * 是否有异常
     */
    private boolean hasErrors;

    /**
     * 异常消息记录
     */
    private List<ValidationErrorNode> errorMsgList;

    /**
     * 获取异常消息组装
     *
     * @return
     */
    public String getMessage() {
        if (CollectionUtils.isEmpty(errorMsgList)) {
            return "";
        }
        StringBuilder message = new StringBuilder();
        errorMsgList.forEach(errorNode -> {
            message.append(MessageFormat.format("{0}:{1} \r\n", errorNode.getParameterName(), errorNode.getParameterMsg()));
        });
        return message.toString();
    }

    @Override
    public String toString() {
        return "ValidationResult{" + "hasErrors=" + hasErrors + ", errorMsg=" + errorMsgList + '}';
    }

}